
Ext.onReady(function(){
	
	var loginName=new Ext.form.TextField({
		fieldLabel:'用户名',
		allowBlank:false
	});
	var loginPassWord=new Ext.form.TextField({
		fieldLabel:'密码',
		allowBlank:false,
		inputType:'password'
	});
	var randCode=new Ext.form.TextField({
		name:'randCode',  
		id:'randCode',  
		fieldLabel:'验证码',
		allowBlank:false
	});
	
	var loginFieldSet=new Ext.form.FieldSet({
//		title: '用户登录',
		 style:'text-align: left;padding:8px;',
		items:[loginName,loginPassWord,randCode]
	});
	
	function validate(){
		return loginName.validate()&&loginPassWord.validate();
	}
	var loginBtn=new Ext.Button({
		text:'登录',
		handler:function(){
			if(!validate()){
				return ;
			}
			Ext.Ajax.request({
				url:'login.do',
				method:'POST',
				params : {
                   sname : loginName.getValue(), 
                   spassword : loginPassWord.getValue(),
                   randCode:randCode.getValue()
                }, 
  				success: function (response) {
  					
  					if(response.responseText=='true'){
  						document.location.href=Ext.get('contextPath').getValue()+"/module/main/main.jsp"; 
  					}
  					else if(response.responseText=='false2'){
  						imgrefresh();
  						Ext.Msg.alert("Error","验证码输入有错或已过期！"); 
  					}
  					else{
  						imgrefresh();
  						Ext.Msg.alert("Error","用户名或密码错误！"); 
  					}
  					
              	}, 
  				failure: function (response) {
                    Ext.Msg.alert("Error","错误"); 
            	}
			});
			
		}
	});

	var resetBtn=new Ext.Button({
		text:'重置',
		handler:function(){
			loginPanel.getForm().reset();
		}
	});
	
	var loginPanel=new Ext.FormPanel({
		frame: true,
        title:'系统登录',    
        width: 390,
        labelAlign:'right',
        renderTo:'loginDIV',
        items:[loginFieldSet],
        buttons:[loginBtn,resetBtn],
        keys:[{
        	key:13,
        	fn:loginBtn.handler
        }]
	});

	 randCoden = Ext.getDom('randCode');    
	 randCodep = Ext.get(randCoden.parentNode);   
	 randCodep.createChild({
	 	tag: 'img',
	 	id:"authImg",
	 	name:"authImg",
	 	src: 'getImg.do',
	 	onclick:'imgrefresh()',
	 	style:'cursor:hand;',
	 	align:'absbottom',
	 	width:80,
	 	height:20
	 });
});

function imgrefresh(){
	Ext.getDom('authImg').src='getImg.do?now='+new Date();
}